use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        // Replace the sample below with your own migration scripts

        manager
            .create_table(
                Table::create()
                    .table(BlockedCrates::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(BlockedCrates::Id)
                            .integer()
                            .not_null()
                            .auto_increment()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(BlockedCrates::CrateName).string().not_null())
                    .col(ColumnDef::new(BlockedCrates::CrateId).integer())
                    .col(ColumnDef::new(BlockedCrates::Version).string().not_null())
                    .col(ColumnDef::new(BlockedCrates::Blocked).boolean().not_null())
                    .col(
                        ColumnDef::new(BlockedCrates::BlockTime)
                            .date_time()
                            .not_null(),
                    )
                    .to_owned(),
            )
            .await?;
        // manager.create_index(sea_query::Index::create().if_not_exists().name("q")).await
        manager
            .create_index(
                Index::create()
                    .unique()
                    .if_not_exists()
                    .name("idx-cratename_version")
                    .table(BlockedCrates::Table)
                    .col(BlockedCrates::CrateName)
                    .col(BlockedCrates::Version)
                    .to_owned(),
            )
            .await?;
        Ok(())
    }

    async fn down(&self, _: &SchemaManager) -> Result<(), DbErr> {
        // Replace the sample below with your own migration scripts
        todo!();
    }
}

#[derive(DeriveIden)]
enum BlockedCrates {
    Table,
    Id,
    CrateName,
    CrateId,
    Version,
    Blocked,
    BlockTime,
}
